﻿using System;
using System.Collections;
using UnityEngine;

namespace ZyGame.UI
{
    [UIOptions("Resources/Wait", UIOptions.UIDepth.Waiting)]
    internal class Wait : UIWindow
    {
        private Coroutine enumerator;

        public void SetWait(string msg, float timeout, Action action)
        {
            IEnumerator WaitTimeout()
            {
                yield return new WaitForSeconds(timeout);
                action?.Invoke();

                Client.Window.CloseView<Wait>();
            }

            if (timeout > 0)
            {
                enumerator = Client.Tools.StartCoroutine(WaitTimeout());
            }

            this.SetText("Text (TMP)", msg);
        }

        public override void Disable()
        {
            base.Disable();
            if (enumerator != null)
            {
                Client.Tools.StopCoroutine(enumerator);
            }

            enumerator = null;
        }

        public override void Dispose()
        {
            base.Dispose();
            if (enumerator != null)
            {
                Client.Tools.StopCoroutine(enumerator);
            }

            enumerator = null;
        }
    }
}